<?php
/**
 * @分销
 * @功能说明:
 * @更新说明: 暂无更新
 * @文件名: CashController.class.php
 * @编码: UTF-8
 * @创建时间: 2016-05-05 12:12
 * @创建人: WENCUN
 */

namespace cash\Controller;


class CashController extends ApibaseController
{
    function __construct() {
        parent::__construct();
        $this->_CustCashModel = D('CustCash');
    }

    /*
     * 财务打款
     * @date: 2016-05-05 12:13:00
     * @editor: WENCUN
     * */
    public function index() {
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $url = array();
        $list = M('CustCash')->where(array('is_check' => '1'));
        if (I('get.cust_realname')) {
            $list = $list->where("cust_realname LIKE '%" . I('get.cust_realname') . "%'");
            $url['cust_realname'] = I('get.cust_realname');
        }
        if (I('get.cust_mobile')) {
            $list = $list->where(array('cust_mobile' => I('get.cust_mobile')));
            $url['cust_mobile'] = I('get.cust_mobile');
        }
        if (I('get.cash_status') != 'all' && I('get.cash_status') != '') {
            $list = $list->where(array('cash_status' => I('get.cash_status')));
            $url['cash_status'] = I('get.cash_status');
        }
        $object = clone $list;
        $getData = $list->order(array('cust_cashid' => 'DESC'))->page($page, $perpage)->select();
        $this->assign('getData', $getData);
        $this->assign('page', setPage($object->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->display('/cash/index');
    }

    /*
     * 提现审核
     * @date: 2016-05-05 14:38:00
     * @editor: WENCUN
     * */
    public function setCheck() {
        if (!in_array(I('post.check'), array(1, -1)) || !I('post.cashid')) {
            jsonRespons(0, '不合法参数');
        }
        $cashEntity = M('CustCash');
        $cashEntity->startTrans();
        $data = array(
            'is_check'  => I('post.check'),
            'update_at' => time()
        );
        if (I('post.reason')) {
            $data['reason'] = I('post.reason');
        }
        $setCash = $cashEntity->lock(true)->where(array('cust_cashid' => I('post.cashid')))
            ->setField($data);
        if ($setCash > 0) {
            if (I('post.check') == '-1') {
                $custBottler = M('CustBottler');
                $custBottler->startTrans();
                $setBottler = $custBottler->lock(true)->where(array('cust_cashid' => I('post.cashid'), 'is_check' => '0', 'cash_status' => '0'))
                    ->setField(array('is_waste' => 1));
                if ($setBottler > 0) {
                    $cashEntity->commit();
                    $custBottler->commit();
                    //@todo 审核不通过微信模板推送
                    $getCash = M('CustCash')->where(array('cust_cashid' => I('post.cashid')))->find();
                    //curl('http://192.168.0.132/jsssdk/applyCash',array(
                    curl('http://wap.hipaygo.cn/jsssdk/applyCash', array(
                        'cust_id'    => $getCash['cust_id'],
                        'cash_money' => $getCash['cash_money'],
                        'is_check'   => $getCash['is_check']
                    ));
                    jsonRespons(1, '操作成功');
                } else {
                    $cashEntity->rollback();
                    $custBottler->rollback();
                    jsonRespons(0, '事务流出错,操作失败');
                }
            }
            $cashEntity->commit();
            jsonRespons(1, '操作成功');
        } else {
            $cashEntity->rollback();
            jsonRespons(0, '事务流出错,操作失败');
        }
    }

    /*
     * 提现打款确认
     * @date: 2016-05-05 16:29:00
     * @editor: WENCUN
     * */
    public function setCash() {
        if (!in_array(I('post.status'), array(1, -1)) || !I('post.cashid')) {
            jsonRespons(0, '不合法参数');
        }
        $cashEntity = M('CustCash');
        $cashEntity->startTrans();
        //获取提现的信息
        $getCashMsg = $cashEntity->field(array('cust_id', 'cash_money'))->where(array('cust_cashid' => I('post.cashid'), 'is_check' => '1', 'cash_status' => '0'))->find();
        $setCash = $cashEntity->lock(true)->where(array('cust_cashid' => I('post.cashid'), 'is_check' => '1', 'cash_status' => '0'))
            ->setField(array('cash_status' => 1, 'update_at' => time()));
        if ($setCash > 0) {
            $cashEntity->commit();
            //提现成功模板消息推送
            curl('http://wap.hipaygo.cn/jsssdk/applyCash', array(
                'cust_id'     => $getCashMsg['cust_id'],
                'cash_money'  => $getCashMsg['cash_money'],
                'is_check'    => '1',
                'cash_status' => '1'
            ));
            jsonRespons(1, '操作成功');
        } else {
            $cashEntity->rollback();
            jsonRespons(0, '事务流出错,操作失败');
        }
    }

    /*
     * 导出财务报表
     * @date: 2016-06-13 16:17:00
     * @editor: GaoChao
     * */
    public function exportExcel() {
        set_time_limit(0);
        if (IS_POST) {
            if (I('post.dosubmit')) {
                $tableHeader = array(
                    '序号ID', '提现金额', '用户姓名', '户名', '开户行', '分行', '账号', '手机号码', '审核时间', '审核状态', '打款状态'
                );
                //$data[0]=$title;
                if (I('post.dotime') && I('post.endtime')) {
                    $doTime = strtotime(I('post.dotime'));
                    $endTime = strtotime(I('post.endtime'));
                    $field = array(
                        'cust_cashid',
                        'cash_money',
                        'cust_realname',
                        'bank_uname',
                        'bank',
                        'bank_name',
                        'bank_card',
                        'cust_mobile',
                        'update_at',
                        'is_check',
                    );
                    $condition = array('add_at' => array(array('lt', $endTime), array('gt', $doTime), 'and'));
                    $shipments = $this->_CustCashModel->getCashList($field, $condition);
                }
                //根据订单编号获取订单商品信息(商品标题,商品数量,商品规格)
                foreach ($shipments as $key => $value) {
                    $data[] = array(
                        '1'  => strval($value['cust_cashid']),
                        '2'  => $value['cash_money'],
                        '3'  => $value['cust_realname'],
                        '4'  => $value['bank_uname'],
                        '5'  => $value['bank'],
                        '6'  => $value['bank_name'],
                        '7'  => $value['bank_card'],
                        '8'  => $value['cust_mobile'],
                        '9'  => $value['update_at'] > 0 ? date('Y/m/d H:i:s', $value['update_at']) : '',
                        '10' => $value['is_check'] > 0 ? '审核通过' : ($value['is_check'] < 0 ? '审核不通过' : '未审核'),
                        '11' => $value['cash_status'] > 1 ? '打款失败' : ($value['cash_status'] < 1 ? '未处理' : '打款成功'),
                    );
                };
                if (!$data) {
                    $this->showtips('没有数据');
                } else {
                    exportExcel($tableHeader, $data);
                }
            }
        }
        $this->display('cash/exportexcel');
    }
}